<?php
/**
 * Created by PhpStorm.
 * User: mww
 * Date: 2018/12/17
 * Time: 16:08
 */
namespace app\api\controller;
use think\Db;
class Myinfo extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
//        header('Access-Control-Allow-Origin:*');
//        extract(input());
//        if($user_id != 0){
//            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//            if($auth == 112){
//                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
//                exit;
//            }elseif($auth == 110){
//                echo json_encode(['status' => 302, 'msg' => $this->frost]);
//                exit;
//            }elseif($auth == 1103){
//                echo json_encode(['status' => 303, 'msg' => $this->del]);
//                exit;
//            }
//        }

    }
    /**
     * 未读消息的数量
     */
    public function haveNoticeRed()
    {
        extract(input());
//        $time = Db::table('up_u_user')
//            ->where('user_id', $user_id)
//            ->value('user_time');
//        $data = Db::table('up_notice')
//            ->where(function ($query) use ($time,$user_id) {
//                $query->where('n_type', 1)->where('n_range', 1)->where('n_send_datetime', '>=', $time)->where('n_del_user_id','not like',"%,$user_id,%")->where('n_read_user_id','not like',"%,$user_id,%");
//            })
//            ->whereOr(function ($query) use ($user_id) {
//                $query->where('n_type', 1)->where('n_range', 2)->where('n_user_id', 'like',"%,$user_id,%")->where('n_del_user_id','not like',"%,$user_id,%")->where('n_read_user_id','not like',"%,$user_id,%");
//            })
//            ->whereOr(function ($query) use ($user_id) {
//                $query->where('n_type', 2)->where('n_user_id', $user_id)->where('n_read_user_id',0);
//            })
//            ->find();

        //1、赞和收藏帖子
        $zan = Db::table('up_zan')
            ->where('zan_read',1)
            ->where('zan_who',$user_id)
            ->count();
        $like_h = Db::table('up_like_hot')->alias('z')
            ->join('up_hot c', 'z.lh_h_id=c.h_id', 'LEFT')
            ->where('lh_read',1)
            ->where('h_user_id',$user_id)
            ->count();
        $one = $zan + $like_h;
        //2、新增关注
        $two = Db::table('up_like')
            ->where('l_read',1)
            ->where('l_other_user_id',$user_id)
            ->count();
        //3、我的评论
        $three = Db::table('up_notice')
            ->where('n_type','<>',1)
            ->where('n_user_id',$user_id)
            ->where('n_read_user_id',0)
            ->count();
        //4、谁打赏了我
        $four = Db::table('up_revenue')
            ->where('revenue_type',2)
            ->where('revenue_other_id',$user_id)
            ->where('revenue_read',1)
            ->count();
        //5、系统消息
        $five = Db::table('up_notice')
            ->where(function ($query) use ($user_id) {
                $query->where('n_type', 1)->where('n_range', 1)->where('n_del_user_id','not like',"%,$user_id,%")->where('n_read_user_id','not like',"%,$user_id,%");
            })
            ->whereOr(function ($query) use ($user_id) {
                $query->where('n_type', 1)->where('n_range', 2)->where('n_user_id', 'like',"%,$user_id,%")->where('n_del_user_id','not like',"%,$user_id,%")->where('n_read_user_id','not like',"%,$user_id,%");
            })
            ->count();
        $red = [
            'one' => $one ,
            'two' => $two ,
            'three' => $three ,
            'four' => $four ,
            'five' => $five ,
        ];
        return json(['status'=>200,'msg'=>$red]);
    }
    /**
     * 获取所有和我有关的消息
     * type：
     * 1、赞和收藏帖子
     * 2、新增关注
     * 3、我的评论
     * 4、谁打赏了我
     * 5、系统消息
     * @return string
     */
    public function getNotice(){
        extract(input());
        if($type == 1) {
            if($left_right == 1){
                $data =  Db::table('up_zan')->alias('z')
                    ->join('up_u_user u', 'z.zan_user_id=u.user_id', 'LEFT')
                    ->where('zan_who',$user_id)
                    ->where('zan_read','<>',3)
                    ->field('z.*,user_id,user_username,user_image')
                    ->order('zan_time DESC')
                    ->page($page, 20)
                    ->select();
                if($data){
                    foreach ($data as $k=>$v){
                        $from = $v['zan_com_type'];
                        $id = $v['zan_com_id'];
                        if($from == 1){
                            $listContent = Db::table('up_comment')->where('comment_id', $id)->value('comment_content');
                        }elseif ($from == 2){
                            $listContent = Db::table('up_comment_tt')->where('comtt_id', $id)->value('comtt_content');
                        }else{
                            $listContent = Db::table('up_hot')->where('h_id', $id)->value('h_content');
                        }
                        $data[$k]['listContent'] = $listContent;
                    }
                }
            }else{
                $data = Db::table('up_like_hot z')
                    ->join('up_hot h', 'z.lh_h_id=h.h_id', 'LEFT')
                    ->join('up_u_user u', 'h.h_user_id = u.user_id', 'left')
                    ->field('z.*, user_username, user_image, user_id, h_id, h_content')
                    ->where('h_user_id',$user_id)
                    ->where('lh_read','<>',3)
                    ->page($page, 20)
                    ->order('lh_time desc')
                    ->select();
            }
        }else if($type == 2) { //新增关注
            $data = Db::table('up_like')->alias('l')
                ->join('up_u_user u', 'l.l_user_id = u.user_id', 'left')
                ->where('l_other_user_id', $user_id)
                ->where('l_read','<>',3)
                ->order('l_time desc')
                ->page($page, 20)
                ->field('l.l_id,l.l_read,l.l_time,u.user_username,u.user_desc,u.user_image,u.user_id')
                ->select();
            //直接进来就已读
            $have = Db::table('up_like')->alias('l')
                ->join('up_u_user u', 'l.l_user_id = u.user_id', 'left')
                ->where('l_other_user_id', $user_id)
                ->where('l_read',1)
                ->find();
            if($have){
                Db::table('up_like')->alias('l')
                    ->join('up_u_user u', 'l.l_user_id = u.user_id', 'left')
                    ->where('l_other_user_id', $user_id)
                    ->where('l_read',1)
                    ->setField('l_read',2);
            }
        }else if($type == 3) { //我的评论（其实是评论了我）
            $data = Db::table('up_notice n')
                ->join('up_u_user u', 'n.n_who = u.user_id', 'left')
                ->where('n_type','<>',1)
                ->where('n_user_id',$user_id)
                ->order('n_send_datetime desc')
                ->page($page, 20)
                ->field('n_id,n_type,n_title,n_send_datetime,n_app_id,n_range,n_content,n_read_user_id,n_see,u.user_username,u.user_image,u.user_id')
                ->select();
        }else if($type == 4) { //谁打赏了我
            $data = Db::table('up_revenue')->alias('l')
                ->join('up_u_user u', 'l.revenue_user_id = u.user_id', 'left')
                ->where('revenue_other_id', $user_id)
                ->where('revenue_read','<>',3)
                ->order('revenue_time desc')
                ->page($page, 20)
                ->field('l.*,u.user_username,u.user_image,u.user_id')
                ->select();
            $have = Db::table('up_revenue')->alias('l')
                ->join('up_u_user u', 'l.revenue_user_id = u.user_id', 'left')
                ->where('revenue_other_id', $user_id)
                ->where('revenue_read',1)
                ->find();
            if($have){
                Db::table('up_revenue')->alias('l')
                    ->join('up_u_user u', 'l.revenue_user_id = u.user_id', 'left')
                    ->where('revenue_other_id', $user_id)
                    ->where('revenue_read',1)
                    ->setField('revenue_read',2);
            }
        }else if($type == 5) {
            $data = Db::table('up_notice')
                ->where(function ($query) use ($user_id) {
                    $query->where('n_type', 1)->where('n_range', 1)->where('n_del_user_id','not like',"%,$user_id,%");
                })
                ->whereOr(function ($query) use ($user_id) {
                    $query->where('n_type', 1)->where('n_range', 2)->where('n_user_id', 'like',"%,$user_id,%")->where('n_del_user_id','not like',"%,$user_id,%");
                })
                ->field('n_title,n_content,n_read_user_id,n_id,n_range,n_send_datetime,n_type')
                ->order('n_send_datetime DESC')
                ->page($page, 20)
                ->select();
            if($data){
                foreach ($data as $k=>$v){
                    if(!strstr($v['n_read_user_id'],",$user_id,")){
                        $data[$k]['n_read_user_id'] = 0;
                    }else{
                        $data[$k]['n_read_user_id'] = 1;
                    }
                }
            }
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 去红点（每一次点击详情都要调一次）
     */
    public function getNoticeInfo(){
        extract(input());
        if($type == 1) {
            if($left_right == 1){
                $data =  Db::table('up_zan')->where('zan_id',$id)->setField('zan_read',2);
            }else{
                $data = Db::table('up_like_hot')->where('lh_id',$id)->setField('lh_read',2);
            }
        }else if($type == 3) { //我的评论（其实是评论了我）
            $data = Db::table('up_notice')->where('n_id',$id)->setField('n_read_user_id',1);
        }else if($type == 5) {
            $notice = Db::table('up_notice')
                ->where('n_id',$id)
                ->find();
            if(!strstr($notice['n_read_user_id'],",$user_id,")){
                $uids = $notice['n_read_user_id'] . $user_id . ',';
                Db::table('up_notice')
                    ->where('n_id', $id)
                    ->setField('n_read_user_id',$uids);
            }
            return json(['status'=>200,'msg'=>$notice]);
        }

        return json(['status'=>200,'msg'=>'ok']);
    }
    /**
     * 删除消息（清空）
     * type：1是系统消息 2是互动消息
     * @return string
     */
    public function delNoticeAll(){
        extract(input());
        if($type == 1) {
            if($left_right == 1){
                $data =  Db::table('up_zan')
                    ->where('zan_who',$user_id)
                    ->where('zan_read','<>',3)
                    ->setField('zan_read',3);
            }else{
                $data = Db::table('up_like_hot z')
                    ->join('up_hot h', 'z.lh_h_id=h.h_id', 'LEFT')
                    ->where('h_user_id',$user_id)
                    ->where('lh_read','<>',3)
                    ->setField('lh_read',3);
            }
        }else if($type == 2) { //新增关注
            $data = Db::table('up_like')->alias('l')
                ->join('up_u_user u', 'l.l_user_id = u.user_id', 'left')
                ->where('l_other_user_id', $user_id)
                ->where('l_read','<>',3)
                ->setField('l_read',3);
        }else if($type == 3) { //我的评论（其实是评论了我）
            $data = Db::table('up_notice n')
                ->where('n_type','<>',1)
                ->where('n_user_id',$user_id)
                ->delete();
        }else if($type == 4) { //谁打赏了我
            $data = Db::table('up_revenue')->alias('l')
                ->where('revenue_other_id', $user_id)
                ->where('revenue_read','<>',3)
                ->setField('revenue_read',3);
        }else if($type == 5) {
            $data = Db::table('up_notice')
                ->where(function ($query) use ($user_id) {
                    $query->where('n_type', 1)->where('n_range', 1)->where('n_del_user_id','not like',"%,$user_id,%");
                })
                ->whereOr(function ($query) use ($user_id) {
                    $query->where('n_type', 1)->where('n_range', 2)->where('n_user_id', 'like',"%,$user_id,%")->where('n_del_user_id','not like',"%,$user_id,%");
                })
                ->field('n_del_user_id,n_id')
                ->select();
            if($data){
                foreach ($data as $k=>$v){
                    $uids = $v['n_del_user_id'] . $user_id . ',';
                    Db::table('up_notice')
                        ->where('n_id', $v['n_id'])
                        ->setField('n_del_user_id',$uids);
                }
            }
        }
        return json(['status'=>200,'msg'=>'操作成功']);
    }
    /**
     * 删除消息：单个删除（待用）
     * @return string
     */
    public function deleteNoticeByIds()
    {
        extract(input());
        $n_id = trim($n_id,',');
        $notice = Db::table('up_notice')
            ->where('n_id','in',$n_id)
            ->field('n_type,n_range,n_del_user_id,n_id')
            ->select();
        foreach ($notice as $k=>$v){
            if($v['n_type'] == 1){
                $uids = $v['n_del_user_id'] . $user_id . ',';
                $res = Db::table('up_notice')
                    ->where('n_id', $v['n_id'])
                    ->setField('n_del_user_id',$uids);
            }else{
                $res = Db::table('up_notice')
                    ->where('n_id',$v['n_id'])
                    ->delete();
            }
        }
        return json(['status'=>200,'msg'=>'操作成功']);
    }

}